package com.example.eureka_client.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eureka_client.entity.CommentsAgree;
import com.example.eureka_client.entity.CommentsInfo;
import com.example.eureka_client.mapper.CommentsAgreeMapper;
import com.example.eureka_client.mapper.CommentsInfoMapper;
import com.example.eureka_client.service.ICommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 父评论表 服务实现类
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Service
public class CommentsInfoServiceImpl extends ServiceImpl<CommentsInfoMapper, CommentsInfo> implements ICommentsInfoService {
    @Resource
    CommentsAgreeMapper commentsAgreeMapper;

    private final static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<CommentsInfo> getAnswerComments(String answerId){
        return baseMapper.getAnswerComments(answerId);
    }

    @Override
    public boolean addComments(CommentsInfo commentsInfo){
        Date nowTime = new Date();
        commentsInfo.setCreateTime(sdf.format(nowTime));
        commentsInfo.setId(UUID.randomUUID().toString());

        if(this.save(commentsInfo)) return true;
        else return false;
    }

    @Override
    public boolean removeById(String id){
        return baseMapper.removeById(id);
    }

    @Override
    public boolean addAgree(CommentsAgree commentsAgree){
        // 更新赞同数
        CommentsInfo commentsInfo = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("LIKE_NUM", commentsInfo.getLikeNum()+1);

        //添加关联
        if(commentsAgreeMapper.insert(commentsAgree) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean addNotAgree(CommentsAgree commentsAgree){
        // 更新踩数
        CommentsInfo commentsInfo = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("NOT_LIKE_NUM", commentsInfo.getNotLikeNum()+1);

        //添加关联
        if(commentsAgreeMapper.insert(commentsAgree) == 1 && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reAgree(CommentsAgree commentsAgree){
        // 更新赞同数
        CommentsInfo commentsInfo = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("LIKE_NUM", commentsInfo.getLikeNum()-1);

        //删除关联
        if(commentsAgreeMapper.deleteById(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean reNotAgree(CommentsAgree commentsAgree){
        // 更新踩数
        CommentsInfo commentsInfo = baseMapper.getById(commentsAgree.getCommentsId());
        UpdateWrapper<CommentsInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", commentsAgree.getCommentsId());
        updateWrapper.set("NOT_LIKE_NUM", commentsInfo.getNotLikeNum()-1);

        //删除关联
        if(commentsAgreeMapper.deleteById(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType()) && this.update(null, updateWrapper)) return true;
        else return false;
    }

    @Override
    public boolean isAgree(CommentsAgree commentsAgree){
        CommentsAgree commentsAgree1 = commentsAgreeMapper.getByThree(commentsAgree.getCommentsId(), commentsAgree.getUserId(), commentsAgree.getType());
        if(commentsAgree1 != null) return true;
        else return false;
    }
}
